package com.topisv.tms.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.topisv.tms.entity.Consignor;
import com.topisv.tms.service.ConsignorService;
import com.topisv.tms.service.CustomersService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/v2/consignor")
public class ConsignorController {
	private static final Logger log = LoggerFactory.getLogger(ConsignorController.class);
	
	@Autowired
	private ConsignorService consignorService;
	@Autowired
	private CustomersService customersService;
	
	@RequestMapping(value = "createConsignor/{id}", method = RequestMethod.GET)
	public String createConsignor(@PathVariable("id") Long id, Model model) {
		Consignor consignor = new Consignor();
		model.addAttribute("consignor", consignor);
		model.addAttribute("action", "createConsignor2/" + id);
		return "customers/customersConsignorForm";
	}
	
	/**
	 * 新增发货人信息
	 * @param consignor
	 * @param id
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "createConsignor2/{id}", method = RequestMethod.POST)
	public String createConsignor2(@Valid Consignor consignor,
			@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
		consignor.setCustomers(customersService.findOne(id));
		consignorService.saveConsignor(consignor);
		redirectAttributes.addFlashAttribute("message", "创建发货人成功");
		return "redirect:/v2/customers/v2_show";
	}
	
	
	@RequestMapping(value = "modifyConsignor/{id}", method = RequestMethod.GET)
	public String modifyConsignor(@PathVariable("id") Long id, Model model) {
		Consignor consignor = consignorService.findConsignor(id);
		model.addAttribute("consignor", consignor);
		model.addAttribute("action", "modifyConsignor2/" + consignor.getCustomers().getId());
		return "customers/customersConsignorForm";
	}
	
	/**
	 * 修改发货人信息
	 * @param consignor
	 * @param id
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "modifyConsignor2/{id}", method = RequestMethod.POST)
	public String modifyConsignor2(
			@Valid @ModelAttribute("consignor") Consignor consignor,
			@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
		log.info("开始修改发货人信息");
		try {
			Consignor oriConsignor = consignorService.findConsignor(consignor.getId());
			consignor.setCustomers(customersService.findOne(id));
			consignor.setDefaultStatus(oriConsignor.getDefaultStatus());
			consignor.setStatus(oriConsignor.getStatus());
			consignorService.saveConsignor(consignor);
			redirectAttributes.addFlashAttribute("message", "修改发货人成功");
			log.info("修改发货人成功");
		} catch (Exception e) {
			redirectAttributes.addFlashAttribute("message", "修改发货人失败");
			log.info("修改发货人失败");
			
		}
	
		return "redirect:/v2/customers/v2_show";
	}
	
	@RequestMapping(value = "consignorDetail")
	public void ConsignorDetail(HttpServletRequest request,
			HttpServletResponse response){
		PrintWriter out = null;
		JSONObject json = null;
		String idString = request.getParameter("id");
		long id =Long.valueOf(idString);
		Consignor consignor = consignorService.findCustomersId(id);
		try {
			out = response.getWriter();
			json = new JSONObject();
			if(null == consignor) {
				json.put("status", "-1");
				json.put("content", "该商家没有发货人信息，是否为该商家新增发货人？");
				out.print(json.toString());
				return;
			}
			else {
				json.put("status", "1");
				json.put("content", consignor);
				out.print(json.toString());
				return;
			}
		} catch (IOException e) {
			
		}
	}
}
